package Thread;
public class demo13All {
    public static void main(String[] args) throws InterruptedException {
        // 创建一个线程 t1
        Thread t1 = new Thread(() -> {
            System.out.println("t1: " + Thread.currentThread().getState()); // NEW

            // 线程开始执行
            System.out.println("t1: Entering RUNNABLE state");
            synchronized (demo13All.class) {
                try {
                    System.out.println("t1: Entering WAITING state");
                    demo13All.class.wait(); // WAITING
                    System.out.println("t1: Exiting WAITING state");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            // 进入 TIMED_WAITING 状态
            System.out.println("t1: Entering TIMED_WAITING state");
            try {
                Thread.sleep(2000); // TIMED_WAITING
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1: Exiting TIMED_WAITING state");

            // 模拟一些工作
            for (int i = 0; i < 5; i++) {
                System.out.println("t1: Working...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("t1: Terminating");
        });

        // 创建另一个线程 t2
        Thread t2 = new Thread(() -> {
            synchronized (demo13All.class) {
                try {
                    System.out.println("t2: Entering BLOCKED state");
                    Thread.sleep(1000); // 模拟一些工作
                    System.out.println("t2: Exiting BLOCKED state");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 打印 t1 的初始状态
        System.out.println("t1: " + t1.getState()); // NEW

        // 启动 t1 线程
        t1.start();
        Thread.sleep(100); // 让 t1 有时间进入 RUNNABLE 状态

        // 打印 t1 的状态
        System.out.println("t1: " + t1.getState()); // RUNNABLE

        // 启动 t2 线程
        t2.start();
        Thread.sleep(100); // 让 t2 有时间进入 RUNNABLE 状态

        // 打印 t2 的状态
        System.out.println("t2: " + t2.getState()); // RUNNABLE

        // 让主线程等待 t1 进入 WAITING 状态
        Thread.sleep(500);

        // 打印 t1 的状态
        System.out.println("t1: " + t1.getState()); // WAITING

        // 让主线程等待 t2 进入 BLOCKED 状态
        Thread.sleep(500);

        // 打印 t2 的状态
        System.out.println("t2: " + t2.getState()); // BLOCKED

        // 通知 t1 线程继续执行
        synchronized (demo13All.class) {
            demo13All.class.notifyAll();
        }

        // 让主线程等待 t1 进入 TIMED_WAITING 状态
        Thread.sleep(1000);

        // 打印 t1 的状态
        System.out.println("t1: " + t1.getState()); // TIMED_WAITING

        // 让主线程等待 t1 和 t2 完成
        t1.join();
        t2.join();

        // 打印 t1 和 t2 的最终状态
        System.out.println("t1: " + t1.getState()); // TERMINATED
        System.out.println("t2: " + t2.getState()); // TERMINATED

        System.out.println("main: All threads have terminated");
    }
}


